
package com.jf.cloud.product.controller.supplier;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.jf.cloud.api.auth.constant.SysTypeEnum;
import com.jf.cloud.api.multishop.bo.EsShopDetailBO;
import com.jf.cloud.api.multishop.constant.ShopStatus;
import com.jf.cloud.api.multishop.feign.ShopDetailFeignClient;
import com.jf.cloud.api.platform.dto.OfflineHandleEventDTO;
import com.jf.cloud.api.platform.feign.ConfigFeignClient;
import com.jf.cloud.api.platform.vo.OfflineHandleEventVO;
import com.jf.cloud.api.product.util.ProductLangUtil;
import com.jf.cloud.api.supplier.bo.SupplierDetailBO;
import com.jf.cloud.api.supplier.feign.SupplierDetailFeignClient;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.constant.StatusEnum;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.product.constant.IsSynUpdate;
import com.jf.cloud.common.product.constant.SpuStatus;
import com.jf.cloud.common.product.constant.SpuType;
import com.jf.cloud.common.product.constant.SupplierSpuType;
import com.jf.cloud.common.product.dto.SpuDTO;
import com.jf.cloud.common.product.vo.*;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.common.util.ExcelUtil;
import com.jf.cloud.common.util.Json;
import com.jf.cloud.product.dto.SpuPageSearchDTO;
import com.jf.cloud.product.listener.SupplierProdExcelListener;
import com.jf.cloud.product.model.SpuExtension;
import com.jf.cloud.product.service.*;
import com.jf.cloud.product.vo.CategoryShopVO;
import com.jf.cloud.product.vo.SpuExcelVO;
import com.jf.cloud.product.vo.SupplierProdExcelVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * spu信息
 *
 * @author zz
 * @date 2020-10-28 15:27:24
 */
@RestController("supplierSpuController")
@RequestMapping("/s/spu")
@Tag(name = "supplier-spu信息")
public class SpuController {

    @Autowired
    private SpuService spuService;
    @Autowired
    private SpuExcelService spuExcelService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private ShopDetailFeignClient shopDetailFeignClient;
    @Autowired
    private SupplierDetailFeignClient supplierDetailFeignClient;
    @Autowired
    private SpuAttrValueService spuAttrValueService;
    @Autowired
    private CategoryShopService categoryShopService;
    @Autowired
    private SpuSupplierChangeLogService spuSupplierChangeLogService;
    @Autowired
    private ConfigFeignClient configFeignClient;
    @Autowired
    private ShopCartService shopCartService;



    @GetMapping
    @Operation(summary = "获取spu信息" , description = "根据spuId获取spu信息")
    public ServerResponseEntity<SpuVO> getBySpuId(@RequestParam Long spuId) {
        // 获取spu信息
        SpuVO spuVO = spuService.getBySpuId(spuId);
        if (!Objects.equals(spuVO.getSupplierId(), AuthUserContext.get().getTenantId()) && !Objects.equals(Constant.PLATFORM_SHOP_ID, AuthUserContext.get().getTenantId())) {
            return ServerResponseEntity.fail(ResponseEnum.UNAUTHORIZED);
        }
        List<SpuAttrValueVO> spuAttrsBySpuId = spuAttrValueService.getSpuAttrsBySpuId(spuId);
        spuVO.setSpuAttrValues(spuAttrsBySpuId);
        SpuExtension spuExtension = spuService.getSpuExtension(spuId);
        spuVO.setTotalStock(spuExtension.getActualStock());
        spuVO.setSaleNum(spuExtension.getSaleNum());
        // 物流模板
        SpuVO.DeliveryModeVO deliveryModeVO = Json.parseObject(spuVO.getDeliveryMode(), SpuVO.DeliveryModeVO.class);
        spuVO.setDeliveryModeVO(deliveryModeVO);
        // 品牌信息
        spuVO.setBrand(brandService.getByBrandId(spuVO.getBrandId()));
        // sku信息
        spuVO.setSkus(skuService.listSkuAllInfoBySpuId(spuId, false));
        loadSpuAttrs(spuVO);
        // 平台分类、供应商分类信息
        spuVO.setCategory(categoryService.getInfo(spuVO.getCategoryId()));
        ProductLangUtil.category(spuVO.getCategory());
        if (Objects.nonNull(spuVO.getSupplierCategoryId()) && !Objects.equals(spuVO.getSupplierCategoryId(), Constant.DEFAULT_ID)) {
            spuVO.setSupplierCategory(categoryService.getInfo(spuVO.getSupplierCategoryId()));
            ProductLangUtil.category(spuVO.getSupplierCategory());
        }
        return ServerResponseEntity.success(spuVO);
    }


    @PostMapping
    @Operation(summary = "保存spu信息" , description = "保存spu信息")
    public ServerResponseEntity<Void> save(@Valid @RequestBody SpuDTO spuDTO) {
        spuDTO.setStatus(StatusEnum.ENABLE.value());
        spuDTO.setSpuType(SpuType.NORMAL.value());
        spuDTO.setSupplierSpuType(SupplierSpuType.YES.value());
        spuDTO.setSpuMold(spuDTO.getSpuMold());
        spuService.save(spuDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新spu信息" , description = "更新spu信息")
    public ServerResponseEntity<Void> update(@Valid @RequestBody SpuDTO spuDTO) {
        SpuVO spuDb = spuService.getBySpuId(spuDTO.getSpuId());
        if (!Objects.equals(spuDb.getSupplierId(), AuthUserContext.get().getTenantId())) {
            return ServerResponseEntity.fail(ResponseEnum.UNAUTHORIZED);
        }
        spuDTO.setSpuMold(spuDTO.getSpuMold());
        spuService.update(spuDTO);
        //  清除购物车数量缓存
        shopCartService.removeShopCartItemCacheBySupplierSpuIds(Collections.singletonList(spuDTO.getSpuId()));
        return ServerResponseEntity.success();
    }


    @DeleteMapping
    @Operation(summary = "删除spu信息" , description = "根据spu信息id删除spu信息")
    public ServerResponseEntity<Void> delete(@RequestParam Long spuId) {
        SpuVO spuDb = spuService.getBySpuId(spuId);
        Long supplierId = AuthUserContext.get().getTenantId();
        if (!Objects.equals(spuDb.getSupplierId(), supplierId) && !Objects.equals(supplierId, Constant.PLATFORM_SHOP_ID)) {
            return ServerResponseEntity.fail(ResponseEnum.UNAUTHORIZED);
        }
        spuService.deleteById(spuId);
        //  清除购物车数量缓存
        shopCartService.removeShopCartItemCacheBySupplierSpuIds(Collections.singletonList(spuId));
        return ServerResponseEntity.success();
    }
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除spu信息" , description = "根据spu信息id删除spu信息")
    public ServerResponseEntity<Void> delete(@RequestParam List<Long> spuIds) {
        Long supplierId = AuthUserContext.get().getTenantId();
        if(CollUtil.isEmpty(spuIds)){
            throw new LuckException("商品id不能为空");
        }
        for(Long spuId : spuIds){
            SpuVO spuDb = spuService.getBySpuId(spuId);
            if (!Objects.equals(spuDb.getSupplierId(), supplierId) && !Objects.equals(supplierId, Constant.PLATFORM_SHOP_ID)) {
                return ServerResponseEntity.fail(ResponseEnum.UNAUTHORIZED);
            }
            spuService.deleteById(spuId);
        }
        //  清除购物车数量缓存
        shopCartService.removeShopCartItemCacheBySupplierSpuIds(spuIds);
        return ServerResponseEntity.success();
    }

    @PutMapping("/update_spu_data")
    @Operation(summary = "修改spu（名称、价格、库存、序号）信息" , description = "更新spu信息")
    public ServerResponseEntity<Void> updateSpuData(@RequestBody SpuDTO spuDTO) {
        SpuVO spuDb = spuService.getBySpuId(spuDTO.getSpuId());
        if (!Objects.equals(spuDb.getSupplierId(), AuthUserContext.get().getTenantId())) {
            return ServerResponseEntity.fail(ResponseEnum.UNAUTHORIZED);
        }
        spuService.updateSpuOrSku(spuDTO);
        //  清除购物车数量缓存
        shopCartService.removeShopCartItemCacheBySupplierSpuIds(Collections.singletonList(spuDTO.getSpuId()));
        return ServerResponseEntity.success();
    }


    /**
     * 更新商品状态
     */
    @PutMapping("/prod_status")
    @Operation(summary = "商品上下架" , description = "商品上下架")
    public ServerResponseEntity<Void> spuChangeStatus(@RequestBody SpuPageSearchDTO spuDTO) {
        if (Objects.nonNull(spuDTO.getSpuId())) {
            spuDTO.setSpuIds(Collections.singletonList(spuDTO.getSpuId()));
        }
        if (CollUtil.isEmpty(spuDTO.getSpuIds())) {
            throw new LuckException("商品id不能为空");
        }
        Long shopId = AuthUserContext.get().getTenantId();
        List<SpuVO> spuList = spuService.listSpuBySpuIds(spuDTO.getSpuIds());

        if(Objects.equals(spuDTO.getStatus(),StatusEnum.ENABLE.value())){
            // 上架商品时，需要根据供应商状态判断是否可以上架商品
            ServerResponseEntity<SupplierDetailBO> supplierRequest = supplierDetailFeignClient.getSupplierBySupplierId(shopId);
            SupplierDetailBO supplierDetailBO = supplierRequest.getData();
            if (!Objects.equals(supplierDetailBO.getSupplierStatus(), ShopStatus.OPEN.value())) {
                // 合同到期
                if(supplierDetailBO.getContractEndTime().compareTo(new Date()) < 0){
                    throw new LuckException("店铺有效期已过，不能进行此操作，请联系管理员后重试");
                }
                throw new LuckException("供应商处于违规下线状态，不能上架商品");
            }
        }
        for (SpuVO spuVO : spuList) {
            if(Objects.equals(spuDTO.getStatus(),StatusEnum.ENABLE.value())) {
                CategoryVO category = categoryService.getInfo(spuVO.getCategoryId());
                if (category == null || Objects.equals(category.getStatus(), StatusEnum.DISABLE.value())) {
                    // 平台分类处于下线中，商品不能上架，请联系管理员后再进行操作
                    throw new LuckException("商品【" + spuVO.getName() + "】的平台分类处于下线中，商品不能上架，请联系管理员后再进行操作");
                }
                //供应商没有店铺分类
               if(!Objects.equals(spuVO.getSpuType(), SpuType.SCORE.value()) && !Objects.equals(AuthUserContext.get().getSysType(), SysTypeEnum.SUPPLIER.value())) {
                    CategoryVO shopCategory = categoryService.getById(spuVO.getShopCategoryId());
                    if (shopCategory == null || Objects.equals(shopCategory.getStatus(), StatusEnum.DISABLE.value())) {
                        // 本店分类处于下线中，商品不能上架
                        throw new LuckException("商品【" + spuVO.getName() + "】的本店分类处于下线中，商品不能上架");
                    }
                }
                // 如果商品正在下架，但又是活动商品，代表这个商品所属的活动被平台下架了
                if (Objects.equals(spuVO.getSpuType(), SpuType.SECKILL.value())) {
                    throw new LuckException("商品【" + spuVO.getName() + "】所属的秒杀活动处于违规下线中，商品不能上架");
                }
                if (Objects.equals(spuVO.getSpuType(), SpuType.GROUP.value())) {
                    throw new LuckException("商品【" + spuVO.getName() + "】所属的团购活动处于违规下线中，商品不能上架");
                }
            }
            if (!Objects.equals(spuVO.getSupplierId(), shopId) && !Objects.equals(shopId, Constant.PLATFORM_SHOP_ID)) {
                return ServerResponseEntity.fail(ResponseEnum.UNAUTHORIZED);
            }
        }
        // 是否开启“商品发布时审核”，若开启，上架修改状态为“待审核”【平台端不用审核】
        String prodSwitchConfig = configFeignClient.getConfig(Constant.SPU_SWITCH_CONFIG).getData();
        if (SpuStatus.PUT_SHELF.value().equals(spuDTO.getStatus())
                && !SysTypeEnum.PLATFORM.value().equals(AuthUserContext.get().getSysType())
                && !ObjectUtils.isEmpty(prodSwitchConfig)) {
            Boolean spuAudit = JSON.parseObject(prodSwitchConfig, new TypeReference<Map<String, Boolean>>(){})
                    .getOrDefault("spuAudit", false);
            if (spuAudit) {
                spuDTO.setStatus(SpuStatus.PUBLISH_WAIT_AUDIT.value());
            }
        }
        spuBatchUpdateStatus(spuDTO);
        for (SpuVO spuVO : spuList) {
            spuService.removeSpuCacheBySpuId(spuVO.getSpuId());
        }
        //  清除购物车数量缓存
        shopCartService.removeShopCartItemCacheBySupplierSpuIds(spuDTO.getSpuIds());
        return ServerResponseEntity.success();
    }

    @GetMapping("/sold_excel")
    @Operation(summary = "导出excel" , description = "导出商品excel")
    public ServerResponseEntity<Void> spuSoldExcel(HttpServletResponse response, SpuPageSearchDTO spuDTO) {
        List<SpuExcelVO> list = spuExcelService.excelSpuList(spuDTO);
        List<SupplierProdExcelVO> supplierProdExcelVOS = list.stream().map(spu -> {
            SupplierProdExcelVO supplierProdExcelVO = new SupplierProdExcelVO();
            supplierProdExcelVO.setSeq(spu.getSeq());
            supplierProdExcelVO.setNameZh(spu.getNameZh());
//            supplierProdExcelVO.setNameEn(spu.getNameEn());
            supplierProdExcelVO.setSpuId(spu.getSpuId());
            supplierProdExcelVO.setSpuCode(spu.getSpuCode());
            supplierProdExcelVO.setStock(spu.getStock());
            supplierProdExcelVO.setPriceFee(spu.getPriceFee());
            supplierProdExcelVO.setMarketPriceFee(spu.getMarketPriceFee());
            supplierProdExcelVO.setVolume(spu.getVolume());
            supplierProdExcelVO.setStatus(spu.getStatus());
            supplierProdExcelVO.setBrandName(spu.getBrandName());
            supplierProdExcelVO.setCategoryName(spu.getCategoryName());
            supplierProdExcelVO.setDeliveryMode(spu.getDeliveryMode());
            supplierProdExcelVO.setDeliveryTemplate(spu.getDeliveryTemplate());
            supplierProdExcelVO.setSkuNameZh(spu.getSkuNameZh());
//            supplierProdExcelVO.setSkuNameEn(spu.getSkuNameEn());
//            supplierProdExcelVO.setPropertiesEn(spu.getPropertiesEn());
            supplierProdExcelVO.setPropertiesZh(spu.getPropertiesZh());
            supplierProdExcelVO.setSellingPointZh(spu.getSellingPointZh());
//            supplierProdExcelVO.setSellingPointEn(spu.getSellingPointEn());
            supplierProdExcelVO.setPartyCode(spu.getPartyCode());
            supplierProdExcelVO.setStockWarning(spu.getStockWarning());
            supplierProdExcelVO.setWeight(spu.getWeight());
            return supplierProdExcelVO;

        }).collect(Collectors.toList());
        ExcelUtil.soleExcel(response, supplierProdExcelVOS, SupplierProdExcelVO.EXCEL_NAME, SupplierProdExcelVO.MERGE_ROW_INDEX, SupplierProdExcelVO.MERGE_COLUMN_INDEX, SupplierProdExcelVO.class);
        return ServerResponseEntity.success();
    }

    @GetMapping("/down_model")
    @Operation(summary = "导出商品excel模板" , description = "导出商品excel模板")
    public ServerResponseEntity<Void> downloadModel(HttpServletResponse response) {
        spuExcelService.downloadModel(response);
        return ServerResponseEntity.success();
    }

    @Operation(summary = "导入文件")
    @PostMapping("/export_excel")
    public ServerResponseEntity<String> exportExcel(@RequestParam("excelFile") MultipartFile file){
        if(file == null) {
            throw new LuckException("网络繁忙，请稍后重试");
        }
        String info = null;
        try {
            Map<Integer, List<String>> errorMap = new HashMap<>(8);
            EasyExcel.read(file.getInputStream(), SupplierProdExcelVO.class, new SupplierProdExcelListener(spuExcelService, errorMap)).sheet().doRead();
            info = spuExcelService.spuExportError(errorMap);
        } catch (IOException e) {
            throw new LuckException(e.getMessage());
        }
        return ServerResponseEntity.success(info);
    }

    /**
     * spu批量上下架
     * @param spuDTO
     */
    private void spuBatchUpdateStatus(SpuPageSearchDTO spuDTO) {
        List<Long> spuIds = spuDTO.getSpuIds();
        Integer status = spuDTO.getStatus();
        spuDTO.setStatus(null);
        List<SpuVO> spuList = spuService.list(spuDTO);
        if (CollUtil.isEmpty(spuList)) {
            throw new LuckException("获取不到该商品信息，请刷新后重试");
        }
        checkUpdateStatusData(spuList, status);
        if (CollUtil.isEmpty(spuIds)) {
            throw new LuckException("您所选择的商品中没有符合操作条件的商品");
        }
        spuService.batchChangeSpuStatus(spuIds, status);
        spuSupplierChangeLogService.insertBatchChangeStatus(spuIds, status);
        //给代销商品增加供应商是商品更新标识
        List<Long> spuIdList = spuService.getSupIdListBySupplierId(spuDTO.getSpuId());
        if(CollUtil.isNotEmpty(spuIdList)){
            spuService.batchChangeSynUpdate(spuIdList, IsSynUpdate.NO_CHANGE.value());
        }
    }

    /**
     * 加载spu属性列表
     * @param spuVO
     */
    private void loadSpuAttrs(SpuVO spuVO) {
        Map<Long, SpuAttrValueVO> attrMap = null;
        if (CollUtil.isNotEmpty(spuVO.getSpuAttrValues())) {
            attrMap = spuVO.getSpuAttrValues().stream().collect(Collectors.toMap(SpuAttrValueVO::getAttrId, s -> s));
        } else {
             attrMap = new HashMap<>(1);
        }
        List<AttrVO> attrList = attrService.getAttrsByCategoryIdAndAttrType(spuVO.getCategoryId());
        List<SpuAttrValueVO> spuAttrValues = new ArrayList<>();
        for (AttrVO attrVO : attrList) {
            SpuAttrValueVO spuAttrValueVO = attrMap.get(attrVO.getAttrId());
            SpuAttrValueVO newSpuAttrValue = new SpuAttrValueVO();
            if (Objects.nonNull(spuAttrValueVO)) {
                boolean hasValue = false;
                for (AttrValueVO attrValue : attrVO.getAttrValues()) {
                    if (Objects.equals(attrValue.getAttrValueId(), spuAttrValueVO.getAttrValueId())) {
                        hasValue = true;
                        break;
                    }
                }
                if (hasValue || CollUtil.isEmpty(attrVO.getAttrValues())) {
                    spuAttrValues.add(spuAttrValueVO);
                    continue;
                }
                newSpuAttrValue.setSpuAttrValueId(spuAttrValueVO.getSpuAttrValueId());
            }
            newSpuAttrValue.setAttrId(attrVO.getAttrId());
            newSpuAttrValue.setAttrName(attrVO.getName());
            newSpuAttrValue.setSearchType(attrVO.getSearchType());
            spuAttrValues.add(newSpuAttrValue);
        }
        spuVO.setSpuAttrValues(spuAttrValues);
    }

    /**
     * 校验spu上下架信息
     * @param list 商品列表
     * @param status 更新的状态
     * @return
     */
    private void checkUpdateStatusData(List<SpuVO> list, Integer status) {
        ProductLangUtil.spuList(list);
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        ServerResponseEntity<EsShopDetailBO> shopRequest = shopDetailFeignClient.getShopByShopId(shopId);
        EsShopDetailBO shopDetail = shopRequest.getData();
        if (Objects.equals(shopDetail.getShopStatus(), ShopStatus.OFFLINE.value()) || Objects.equals(shopDetail.getShopStatus(), ShopStatus.OFFLINE_AWAIT_AUDIT.value())) {
            throw new LuckException("店铺处于违规下线中，不能修改商品");
        }
        for (SpuVO spu : list) {
            if (!Objects.equals(spu.getSupplierId(), shopId) && !Objects.equals(AuthUserContext.get().getTenantId(), Constant.PLATFORM_SHOP_ID)) {
                throw new LuckException(ResponseEnum.UNAUTHORIZED);
            }
            // 商品状态不为启用或未启用状态
            if (!Objects.equals(spu.getStatus(), StatusEnum.ENABLE.value())
                    && !Objects.equals(spu.getStatus(), StatusEnum.DISABLE.value())) {
                throw new LuckException("商品(" + spu.getName() + ")不能进行上下架操作");
            }
            if(Objects.equals(status, StatusEnum.ENABLE.value()) && !Objects.equals(spu.getSpuType(), SpuType.SCORE.value())){
                CategoryVO category = categoryService.getById(spu.getCategoryId());
                if (!Objects.equals(category.getStatus(), StatusEnum.ENABLE.value())){
                    throw new LuckException("商品(" + spu.getName() +")所属的平台分类处于下线中，商品不能进行上架操作，请联系管理员后再进行操作");
                }
                //供应商没有店铺分类
                if(!Objects.equals(spu.getSpuType(), SpuType.SCORE.value())) {
                    if(!Objects.equals(sysType, SysTypeEnum.SUPPLIER.value())){
                        CategoryVO shopCategory = categoryService.getById(spu.getShopCategoryId());
                        if (!Objects.equals(shopCategory.getStatus(), StatusEnum.ENABLE.value())) {
                            throw new LuckException("商品(" + spu.getName() + ")所属的店铺分类处于下线中，商品不能进行上架操作");
                        }
                    }
                    CategoryShopVO categoryShopVO = categoryShopService.getByShopIdAndCategoryId(Objects.equals(sysType, SysTypeEnum.SUPPLIER.value()) ? spu.getSupplierId() : spu.getShopId(), spu.getCategoryId(), sysType);
                    if (Objects.isNull(categoryShopVO)) {
                        throw new LuckException("商品(" + spu.getName() + ")所属的平台分类与店铺的签约关系已失效，商品不能进行上架操作");
                    }
                }
            }
        }
    }

    @PostMapping("/offline")
    @Operation(summary = "下线商品" , description = "下线商品")
    public ServerResponseEntity<Void> offline(@RequestBody OfflineHandleEventDTO offlineHandleEventDto) {
        if (!Objects.equals(Constant.PLATFORM_SHOP_ID, AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        SpuVO spuVO = spuService.getBySpuId(offlineHandleEventDto.getHandleId());
        if (Objects.isNull(spuVO)) {
            throw new LuckException(ResponseEnum.DATA_ERROR);
        }
        spuService.offline(offlineHandleEventDto);
        spuService.removeSpuCacheBySpuId(offlineHandleEventDto.getHandleId());
        return ServerResponseEntity.success();
    }

    @PostMapping("/batch_offline")
    @Operation(summary = "批量下线商品" , description = "批量下线商品")
    public ServerResponseEntity<Void> batchOffline(@RequestBody OfflineHandleEventDTO offlineHandleEventDto) {
        if (!Objects.equals(Constant.PLATFORM_SHOP_ID, AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        if (CollUtil.isEmpty(offlineHandleEventDto.getHandleIds())) {
            throw new LuckException("下线的商品id列表不能为空");
        }
        spuService.batchOffile(offlineHandleEventDto);
        return ServerResponseEntity.success();
    }

    @GetMapping("/get_offline_handle_event/{spuId}")
    @Operation(summary = "获取最新下线的事件" , description = "获取最新下线的事件")
    public ServerResponseEntity<OfflineHandleEventVO> getOfflineHandleEvent(@PathVariable Long spuId) {
        SpuVO spuDb = spuService.getBySpuId(spuId);
        if (!Objects.equals(spuDb.getSupplierId(), AuthUserContext.get().getTenantId()) && !Objects.equals(Constant.PLATFORM_SHOP_ID, AuthUserContext.get().getTenantId()) ) {
            return ServerResponseEntity.fail(ResponseEnum.UNAUTHORIZED);
        }
        return ServerResponseEntity.success(spuService.getOfflineHandleEvent(spuId));
    }

//    @GetMapping("/page_can_seckill_prod")
//    @Operation(summary = "获取可以参与秒杀活动的商品分页列表" , description = "可以参与秒杀活动的商品分页列表")
//    public ServerResponseEntity<PageVO<SpuVO>> pageCanSeckillProd(@Valid PageDTO pageDTO,SpuDTO spuDTO) {
//        spuDTO.setShopId(AuthUserContext.get().getTenantId());
//        return ServerResponseEntity.success(spuService.pageCanSeckillProd(pageDTO, spuDTO));
//    }

    @PostMapping("/audit")
    @Operation(summary = "审核商品" , description = "审核商品")
    public ServerResponseEntity<Void> audit(@RequestBody OfflineHandleEventDTO offlineHandleEventDto) {
        if (!Objects.equals(Constant.PLATFORM_SHOP_ID, AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        spuService.audit(offlineHandleEventDto);
        spuService.removeSpuCacheBySpuId(offlineHandleEventDto.getHandleId());
        return ServerResponseEntity.success();
    }

    @PostMapping("/audit_apply")
    @Operation(summary = "违规活动提交审核" , description = "违规活动提交审核")
    public ServerResponseEntity<Void> auditApply(@RequestBody OfflineHandleEventDTO offlineHandleEventDto) {
        SpuVO spuDb = spuService.getBySpuId(offlineHandleEventDto.getHandleId());
        if (!Objects.equals(spuDb.getSupplierId(), AuthUserContext.get().getTenantId())) {
            return ServerResponseEntity.fail(ResponseEnum.UNAUTHORIZED);
        }
        spuService.auditApply(offlineHandleEventDto);
        return ServerResponseEntity.success();
    }

    @GetMapping("/stockWarning_count")
    @Operation(summary = "库存预警数目",description = "库存预警数目")
    public ServerResponseEntity<Long> stockWarningCount(){
        Long supplierId = AuthUserContext.get().getTenantId();
        return ServerResponseEntity.success(spuService.stockWarningCount(supplierId));
    }

}
